#include "woodtypeform.h"
#include "ui_woodtypeform.h"
#include <QGraphicsDropShadowEffect>
#include <QMessageBox>
#include <QSettings>
#include <QTextCodec>

WoodTypeForm::WoodTypeForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::WoodTypeForm)
{
    ui->setupUi(this);
    InitPage();

    //内层窗口添加对应的阴影效果
    QGraphicsDropShadowEffect *shadow_effectA = new QGraphicsDropShadowEffect(this);

    shadow_effectA->setOffset(0, 0);
    shadow_effectA->setColor(QColor(150,150,150));
    shadow_effectA->setBlurRadius(6);

    ui->woodType_gBox_1->setGraphicsEffect(shadow_effectA);

}

WoodTypeForm::~WoodTypeForm()
{
    delete ui;
}


void WoodTypeForm::InitPage()
{
    ui->tableWidget->setAlternatingRowColors(true);
    // set table export
    ui->tableWidget->setShowGrid(true);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//整行选中的方式
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);//可以选中单个
    ui->tableWidget->verticalHeader()->hide();//取消默认行号


    QStringList headerExport;
    headerExport<< tr("Index ")<< tr("Name");

    ui->Bt_Add->show();
    ui->Bt_Delete->show();


    ui->tableWidget->setColumnCount(headerExport.size());
    ui->tableWidget->setHorizontalHeaderLabels(headerExport);
    RdConfig();
}

void WoodTypeForm::RdConfig()
{
    QString Path = "configs/configs.ini";
    QSettings iniRead(Path, QSettings::IniFormat);
    iniRead.setIniCodec(QTextCodec::codecForName("UTF_8"));

    QString info;
    int nNum = iniRead.value("Type/num").toInt();
    ui->tableWidget->setRowCount(nNum); // 设置显示的行数



    for (int i=0;i<nNum;i++) {
        QTableWidgetItem* Item = new QTableWidgetItem(QString::number(i + 1));
        Item->setFlags(Item->flags() & (~Qt::ItemIsEditable));//设置不可编辑
        Item->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        ui->tableWidget->setItem(i, 0, Item);


        info = QString("WoodType/part_%1").arg(i+1);
        Item = new QTableWidgetItem(iniRead.value(info).toString());
        ui->tableWidget->setItem(i, 1, Item);
        Item->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    }
}

void WoodTypeForm::WdConfig()
{
    QString Path = "configs/configs.ini";
    QSettings iniWrite(Path, QSettings::IniFormat);
    iniWrite.setIniCodec(QTextCodec::codecForName("UTF_8"));
    int rowCount = ui->tableWidget->rowCount();
    iniWrite.setValue("Type/num", rowCount);
    for (int i=0;i<rowCount;i++) {
        iniWrite.setValue("WoodType/part_"+QString::number(i+1),  ui->tableWidget->item(i,1)->text());
    }
}



void WoodTypeForm::on_Bt_Add_clicked()
{
    int RowCont;
    RowCont=ui->tableWidget->rowCount();
    ui->tableWidget->insertRow(RowCont);//增加一行


    QTableWidgetItem* Item = new QTableWidgetItem(QString::number(RowCont+1));
    Item->setFlags(Item->flags() & (~Qt::ItemIsEditable));//设置不可编辑
    ui->tableWidget->setItem(RowCont,0,Item);

    ui->tableWidget->setItem(RowCont,1,new QTableWidgetItem());


    ui->tableWidget->item(RowCont,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    ui->tableWidget->item(RowCont,1)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
}


void WoodTypeForm::on_Bt_Delete_clicked()
{
    int RowCont;
    RowCont=ui->tableWidget->rowCount();
    if (RowCont!= 0)
    {
        ui->tableWidget->removeRow(RowCont-1);
    }
}


void WoodTypeForm::on_Bt_Save_clicked()
{
    WdConfig();
    QMessageBox::information(this, tr("info"), tr("Save exports success!"), tr("Yes"), tr("No"));
}

